package runFromHere;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import ccrm.CCRMServer;

import javax.swing.JPasswordField;

import entitys.Session;
import java.awt.Color;

public class ServerGUI {

	private JFrame frmCcrmserver;
	private JTextField portTextField;
	private JPasswordField passwordField;
	private JTextField usernameTextField;
	private JLabel lblOnline;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerGUI window = new ServerGUI();
					window.frmCcrmserver.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ServerGUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCcrmserver = new JFrame();
		frmCcrmserver.setResizable(false);
		frmCcrmserver.setTitle("CCRMServer");
		frmCcrmserver.setBounds(100, 100, 232, 176);
		frmCcrmserver.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmCcrmserver.getContentPane().setLayout(null);
		portTextField = new JTextField();
		portTextField.setText("5555");
		portTextField.setBounds(93, 8, 86, 20);
		frmCcrmserver.getContentPane().add(portTextField);
		portTextField.setColumns(10);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setBounds(10, 11, 46, 14);
		frmCcrmserver.getContentPane().add(lblPort);
		
		JButton btnStartListening = new JButton("Start Listening");

		btnStartListening.setBounds(37, 113, 142, 23);
		frmCcrmserver.getContentPane().add(btnStartListening);
		
		JLabel lblDbPassword = new JLabel("DB password:");
		lblDbPassword.setBounds(10, 61, 86, 14);
		frmCcrmserver.getContentPane().add(lblDbPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(93, 58, 86, 20);
		passwordField.setText("1234");
		frmCcrmserver.getContentPane().add(passwordField);
		
		JLabel lblDbUsername = new JLabel("DB username:");
		lblDbUsername.setBounds(10, 36, 86, 14);
		frmCcrmserver.getContentPane().add(lblDbUsername);
		
		usernameTextField = new JTextField();
		usernameTextField.setText("root");
		usernameTextField.setBounds(93, 33, 86, 20);
		frmCcrmserver.getContentPane().add(usernameTextField);
		usernameTextField.setColumns(10);
		
		lblOnline = new JLabel("ONLINE");
		lblOnline.setBounds(81, 86, 46, 14);
		frmCcrmserver.getContentPane().add(lblOnline);
		lblOnline.setVisible(false);
		
		btnStartListening.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Session.setPort(Integer.parseInt(portTextField.getText()));
				Session.setDbPwd(passwordField.getText());
				Session.setDbUsr(usernameTextField.getText());
				while(Session.getDbPwd()==null||Session.getDbUsr()==null||Session.getPort()==-1);
				CCRMServer server= new CCRMServer(Integer.parseInt(portTextField.getText()));
				 try
				 {
					  
					  server.connect();
					  lblOnline.setVisible(true);
					  lblOnline.setForeground(Color.GREEN);
					  btnStartListening.setEnabled(false);
					  portTextField.setEditable(false);
					  usernameTextField.setEditable(false);
					  passwordField.setEditable(false);
					}
				    catch (Exception ex) 
				    {
				    	System.out.println("ERROR - Could not listen for clients!");
				    
				    }
				
			}
		});
		
	}
}
